/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Admin.DAO;

import Admin.Model.DirectoryCategory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author home
 */
public class DirectoryCategoryDAO {

     private Connection myCon;
    private PreparedStatement ps;
    private ResultSet rs;
    private String strCommand = "";

    public ArrayList<DirectoryCategory> GetAll() {
        ArrayList<DirectoryCategory> listDirectoryCategory = new ArrayList<DirectoryCategory>();
        DirectoryCategory directoryCategory = null;
        try {

            strCommand = "select * from tbl_DirectoryCategories";
            myCon = myConnection.getConnection();
            myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand);

            rs = ps.executeQuery();
            while (rs.next()) {
                directoryCategory=new DirectoryCategory();
                directoryCategory.setDirectoryCategoryID(rs.getInt("directoryCategoryID"));
                directoryCategory.setDirectory_categoryName(rs.getString("directory_categoryName"));
                directoryCategory.setDiCategoriesStatus(rs.getInt("diCategoriesStatus"));

                listDirectoryCategory.add(directoryCategory);

            }
            return listDirectoryCategory;
        } catch (SQLException ex) {
            Logger.getLogger(DirectoryCategoryDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            myConnection.closeConnection(myCon, ps, rs);
        }
        return listDirectoryCategory;
    }

    public boolean AddDirectoryCategory(String directory_categoryName) {
        boolean flag = false;
        try {

            strCommand = "INSERT INTO tbl_DirectoryCategories (directory_categoryName,diCategoriesStatus) VALUES (?,1)";
            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand);
            ps.setString(1, directory_categoryName);
            int i = ps.executeUpdate();
            if (i > 0) {
                flag = true;
            }

        } catch (SQLException ex) {
            Logger.getLogger(DirectoryCategoryDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            myConnection.closeConnection(myCon, ps, null);
        }
        return flag;
    }

    public DirectoryCategory GetByID(int directoryCategoryID) {

        DirectoryCategory directoryCategory = new DirectoryCategory();
        try {

            strCommand = "select * from tbl_DirectoryCategories WHERE directoryCategoryID=?";

            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand);
            ps.setInt(1, directoryCategoryID);
            rs = ps.executeQuery();
            while (rs.next()) {
            directoryCategory.setDirectoryCategoryID(rs.getInt("directoryCategoryID"));
            directoryCategory.setDirectory_categoryName(rs.getString("directory_categoryName"));
            directoryCategory.setDiCategoriesStatus(rs.getInt("diCategoriesStatus"));
            }
            return directoryCategory;
        } catch (SQLException ex) {
            Logger.getLogger(DirectoryCategoryDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            myConnection.closeConnection(myCon, ps, rs);
        }
        return directoryCategory;
    }

    public boolean changeStatus(int directoryCategoryID, int diCategoriesStatus) {
        boolean flag = false;
        try {
            strCommand = "UPDATE tbl_DirectoryCategories SET diCategoriesStatus = ? WHERE directoryCategoryID = ?";
            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ps.setInt(1, diCategoriesStatus);
            ps.setInt(2, directoryCategoryID);
            int i = ps.executeUpdate();
            if (i > 0) {
                flag = true;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            myConnection.closeConnection(myCon, ps, null);
        }
        return flag;
    }

    public boolean editDirectoryCategory(DirectoryCategory objDirectoryCategory) {
        //UPDATE tbl_DirectoryCategories
        boolean flag = false;
        try {
            strCommand = "UPDATE tbl_DirectoryCategories SET directory_categoryName = ? WHERE directoryCategoryID = ?";
            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ps.setString(1, objDirectoryCategory.getDirectory_categoryName());
            ps.setInt(2, objDirectoryCategory.getDirectoryCategoryID());
            int i = ps.executeUpdate();
            if (i > 0) {
                flag = true;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            myConnection.closeConnection(myCon, ps, null);
        }
        return flag;
    }
    public boolean deleteDirectoryCategory(int directoryCategoryID)
    {
         boolean flag = false;
        try {
            strCommand = "DELETE FROM tbl_DirectoryCategories WHERE directoryCategoryID = ?";
            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ps.setInt(1, directoryCategoryID);
            int i = ps.executeUpdate();
            if (i > 0) {
                flag = true;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            myConnection.closeConnection(myCon, ps, null);
        }
        return flag;
    }
}
